<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <!-- 下拉菜单 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="!data.children.length" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <!-- 实现新增子部门或编辑部门的对话框 -->
      <el-dialog
        :title="isEdit?'新增子部门':'编辑部门'"
        :visible.sync="showDialog"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
      >
        <!-- 在弹层中放入子组件 -->
        <deptDialog
          :id="curId"
          ref="addOrEditRef"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="showDialog=false"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import deptDialog from './deptDialog.vue'
import { getDepartments, deleteDepartment } from '@/api/departments'
import { tranDataToTree } from '@/utils/index'
export default {
  components: {
    deptDialog
  },
  data() {
    return {
      curId: '',
      isEdit: false,
      // 依赖一份树形数据
      list: [],
      showDialog: false, // 控制弹框开启关闭
      originList: [] // 保存所有部门列表部分（id,pid,name,code)
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 加载部门列表

    // 删除部门
    hDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否删除该部门?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        await deleteDepartment(id)
        // 重新渲染数据
        this.loadDepartments()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hSuccess() {
      // 重新渲染
      this.loadDepartments()
      // 关闭弹层
      this.showDialog = false
    },
    // 编辑部门
    hEdit(id) {
      this.curId = id
      this.isEdit = true
      this.showDialog = true
      // 关闭显示子组件
      this.$nextTick(() => {
        this.$refs.addOrEditRef.loadDepartmentDetail()
      })
    },
    hAdd(id) {
      this.curId = id
      this.showDialog = true
    },
    // 获取树形结构列表 保存到list
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log('getDepartments', res)
        // 获取部门数据并取出，数组中每个对象的部门数据
        this.originList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, name: item.name, code: item.code }
        })
        // 删除数组中第一条数据
        res.data.depts.shift()
        this.list = tranDataToTree(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
